<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="icon" href="imags/22.jpg">
</head>

<body style="background:url(/imags/3.jpg); background-size:100% 100% ; background-attachment: fixed">
    <div class="container">
        <div class="row align-items-center">
            <div class="col"></div>
            <div class="col">
                <div class="alert alert-danger text-center invisible" role="alert" style="width: 42rem;">
                    <strong>提示：</strong>
                    <font id="msgId">提示</font>
                </div>
            </div>
            <div class="col"></div>
        </div>

        <div class="row align-items-center" style="height: 30rem;">
            <div class="col">

            </div>
            <div class="col">
                <div class="card shadow rounded" style="width: 42rem;opacity: 0.7;">
                    <div class="card-body align-items-center">
                        <h2 class="align-middle" style="margin-left: 16rem;
                        margin-bottom: 2rem;">注册</h2>
                        <form id="registerForm" action="/registerDone" method="POST">

                            <div class="form-group row" style="margin-left: 5rem;">
                                <label class="col-2" for="exampleInputEmail1">用户名：</label>
                                <input type="text" class="form-control col-6" name="username" id="username"
                                    aria-describedby="emailHelp" placeholder="请输入用户名">
                                <small id="comment" class="form-text text-muted" style="display: none;">{{msg}}</small>
                            </div>
                            <div class="form-group row" style="margin-left: 5rem;">
                                <label class="col-2" for="exampleInputPassword1">密码：</label>
                                <input type="password" class="form-control col-6" name="password" id="password"
                                    placeholder="请输入密码">
                            </div>
                            <div class="form-group row" style="margin-left: 5rem;">
                                <label class="col-2" for="exampleInputPassword1" style="padding: 1px;">确认密码：</label>
                                <input type="password" class="form-control col-6" name="confirmpassword"
                                    id="confirmpassword" placeholder="请确认密码">
                            </div>

                            <button type="button" class="btn btn-primary" style=" margin-left: 10rem;"
                                onclick="jump()">注册</button>
                            <button type="button" class="btn btn-primary" style=" margin-left: 10rem;"
                                onclick="cancel()">取消</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col">

            </div>
        </div>

    </div>

    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script>
        function jump() {
            let uid = $('#username').val();
            let pwd = $('#password').val();
            let confirmpwd = $('#confirmpassword').val();

            if (uid.length > 0 && pwd.length > 0 && pwd === confirmpwd) {
                axios({
                    method: 'post',
                    url: '/registerDone',
                    data: $('#registerForm').serialize(),
                }).then(function (res) {
                    res = res.data;
                    if (res.code === 200) {
                        window.location.href = '/login';
                    } else {
                        alert_fn(res.msg);
                        console.log(res.msg);
                    }
                }).catch((err) => {
                    alert_fn(err);
                    console.log(err);
                });
            } else {
                alert_fn('用户名、密码不能为空，并且两次密码应该一致');
            }
        }

        function alert_fn(msg) {
            $('#msgId').text(msg);
            $('.alert').removeClass('invisible');
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
        function cancel() {
            window.location.href = '/login'
        }
    </script>
</body>

</html>